@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">新增出入库记录</h5>
                </div>

                <div class="card-body">
                    @if(session('error'))
                        <div class="alert alert-danger">
                            {{ session('error') }}
                        </div>
                    @endif

                    <form action="{{ route('inventory.store') }}" method="POST">
                        @csrf

                        <div class="form-group">
                            <label for="material_id">材料</label>
                            <select name="material_id" id="material_id" class="form-control @error('material_id') is-invalid @enderror" required>
                                <option value="">请选择材料</option>
                                @foreach($materials as $material)
                                    <option value="{{ $material->id }}" {{ old('material_id') == $material->id ? 'selected' : '' }}>
                                        {{ $material->name }} (当前库存: {{ $material->stock }})
                                    </option>
                                @endforeach
                            </select>
                            @error('material_id')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="form-group mt-3">
                            <label for="type">操作类型</label>
                            <select name="type" id="type" class="form-control @error('type') is-invalid @enderror" required>
                                <option value="">请选择操作类型</option>
                                <option value="in" {{ old('type') === 'in' ? 'selected' : '' }}>入库</option>
                                <option value="out" {{ old('type') === 'out' ? 'selected' : '' }}>出库</option>
                            </select>
                            @error('type')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="form-group mt-3">
                            <label for="quantity">数量</label>
                            <input type="number" name="quantity" id="quantity" class="form-control @error('quantity') is-invalid @enderror" 
                                   value="{{ old('quantity') }}" step="0.01" min="0.01" required>
                            @error('quantity')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="form-group mt-3">
                            <label for="remark">备注</label>
                            <textarea name="remark" id="remark" class="form-control @error('remark') is-invalid @enderror" 
                                      rows="3">{{ old('remark') }}</textarea>
                            @error('remark')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="form-group mt-4">
                            <button type="submit" class="btn btn-primary">提交</button>
                            <a href="{{ route('inventory.index') }}" class="btn btn-secondary">返回</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection 